'''

Case Type： 数据库系统
Case Name： 备机不可读，gs_ctl restart重启集群
Create At:  2020/07/21
@zou_jialiang0505328126
Descption:  1.关闭主备机的备机可读2.重启集群3.查询集群状态(主备)4.恢复主备机的备机可读5.重复步骤1~4 50次6.执行TPCC事务

history：
created： 2020/07/21  created by @wan005
'''
import os
import unittest
from yat.test import Node
import time
import _thread
from yat.test import macro
import sys

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *

logger = Logger()
common = Common()


class GsCtlRestart(unittest.TestCase):
    dbPrimaryUserNode = Node(node='PrimaryDbUser')
    dbPrimaryRootNode = Node(node='PrimaryRoot')
    DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
    DB_ENV_PATH = macro.DB_ENV_PATH
    RESTART_SUCCESS_MSG = Constant.RESTART_SUCCESS_MSG
    GSGUC_SUCCESS_MSG = Constant.GSGUC_SUCCESS_MSG
    STANDBY_NORMAL = Constant.STANDBY_NORMAL
    PRIMARY_NORMAL = Constant.PRIMARY_NORMAL
    TPCC_PATH = macro.TPCC_PATH
    TPCC_SUCCESS_MSG = Constant.TPCC_SUCCESS_MSG
    commsh = CommonSH()
    standby_1_sh = CommonSH('Standby1DbUser')
    standby_2_sh = CommonSH('Standby2DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info("-----------this is setup-----------")
        logger.info('------------Opengauss_Reliability_Dbsys_Case122.py start------------')

    def test_gs_ctl_restart(self):
        for i in range(1, 10):
            logger.info('------------------round ' + str(i) + ' ------------------')

            # set standby node1 to be unreadable
            logger.info("-----------set standby node1 to be unreadable-----------")
            result = self.commsh.executeGsguc('set', self.GSGUC_SUCCESS_MSG, 'hot_standby=off')
            self.assertTrue(result)

            logger.info("-----------restart opengauss-----------")
            result = self.commsh.executeGsctl('restart', self.RESTART_SUCCESS_MSG)
            self.assertTrue(result)
            time.sleep(60)

            logger.info("-----------check opengauss status-----------")
            result = self.commsh.getDbClusterStatus()
            self.assertTrue(result)

            logger.info("-----------set standby node1 to be readable-----------")
            result = self.commsh.executeGsguc('set', self.GSGUC_SUCCESS_MSG, 'hot_standby=on')
            self.assertTrue(result)
            result = self.commsh.executeGsctl('restart', self.RESTART_SUCCESS_MSG)
            self.assertTrue(result)

        logger.info('--------------------start TPCC-------------------------')
        tpcc_result = common.startTPCC(self.dbPrimaryRootNode, self.TPCC_PATH)

        self.assertTrue(tpcc_result.find(self.TPCC_SUCCESS_MSG) > -1)
        logger.info('-------------' + tpcc_result + '-----------------------')

    def tearDown(self):
        logger.info('----------------this is tearDown-----------------------')
        self.commsh.check_whether_remain_dirty_page()
        self.standby_1_sh.check_data_consistency()
        self.standby_2_sh.check_data_consistency()
